﻿#include "main_widget.h"
#ifdef Q_OS_ANDROID
#include "ui_main_widget_mobile.h"
#else
#include "ui_main_widget.h"
#endif
#include "global_manager.h"
#include <QTabBar>
#include <QLabel>
#include <QHBoxLayout>
#include <QPixmap>
#include <QFile>
#include "doc_edit_widget.h"

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
    , m_planManagerWidget(new PlanManagerWidget(this))
    , m_prjManagerWidget(new PrjManagerWidget(this))
    , m_cfgManagerWidget(new ConfigureManagerWidget(this))
    , m_wikiManagerWidget(new WikiManagerWidget(this))
{
    ui->setupUi(this);
    initUi();
    initEvents();
    this->setWindowTitle(QStringLiteral("高效人生"));
    GlobalManager::getInstance()->initTimer();
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::initUi()
{
    ui->stackedWidget->addWidget(m_planManagerWidget);
    ui->stackedWidget->addWidget(m_prjManagerWidget);
    ui->stackedWidget->addWidget(m_cfgManagerWidget);
    ui->stackedWidget->addWidget(m_wikiManagerWidget);

    /**< 默认首页是项目管理页 */
    ui->stackedWidget->setCurrentWidget(m_prjManagerWidget);
    ui->prjManagerBtn->setChecked(true);

    /**< 定制样式 */
    QFile styleFile(":/resources/qss/style.qss");
    styleFile.open(QIODevice::ReadOnly);
    QString stylesheet = styleFile.readAll();
    styleFile.close();

    /**< 通用样式 */
    QFile globalStyleFile(":/resources/qss/global_style.qss");
    globalStyleFile.open(QIODevice::ReadOnly);
    QString globalStyleSheet = globalStyleFile.readAll();
    globalStyleFile.close();

    setStyleSheet(globalStyleSheet + stylesheet);
}

void MainWidget::initEvents()
{
    connect(ui->planManagerBtn, &QPushButton::clicked, this, [=](bool checked) {
        if (checked) {
            ui->stackedWidget->setCurrentWidget(m_planManagerWidget);
        }
    });

    connect(ui->prjManagerBtn, &QPushButton::clicked, this, [=](bool checked) {
        if (checked) {
            ui->stackedWidget->setCurrentWidget(m_prjManagerWidget);
        }
    });

    connect(ui->cfgManagerBtn, &QPushButton::clicked, this, [=](bool checked) {
        if (checked) {
            ui->stackedWidget->setCurrentWidget(m_cfgManagerWidget);
        }
    });

    connect(ui->wikiManagerBtn, &QPushButton::clicked, this, [=](bool checked) {
        if (checked) {
            ui->stackedWidget->setCurrentWidget(m_wikiManagerWidget);
        }
    });
}
